package com.example.spinning.service;

import com.example.spinning.bean.*;
import com.example.spinning.mapper.OrganMapper;
import com.example.spinning.mapper.PrivMapper;
import com.example.spinning.mapper.RoleMapper;
import com.example.spinning.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 角色管理
 */
@Service
@Transactional
public class RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PrivMapper privMapper;
    @Autowired
    UsersMapper usersMapper;

//查询所有角色分页
    public List<Role> getAllRole(int page,int count,Role role) {
        int start = (page-1)*count;
        return roleMapper.getAllRoles(start,count,role);
    }
    //查询所有角色没分页
    public List<Role> getAllRole() {

        return roleMapper.getAllRolesDown();
    }

//删除角色
    public boolean deleteRoleByIds(String ids) {
        String[] split = ids.split(",");
        List<Long> list_Id = new ArrayList();
        int length = split.length;
        for(int i=0;i<length;i++){
//            查询角色与权限的关系
            List<Role> list = roleMapper.getRolePrivByRole(Integer.parseInt(split[i]));

            if(list.size()>0){
                String [] idss=new String[list.size()];
                for(int j=0;j<list.size();j++){
                    idss[j]=list.get(j).getRoleId().toString();
                }

                roleMapper.deleteRolesPrivs(idss);
            }

//            查询用户与角色的关系
            List<User> list2 = usersMapper.getUserRole(split[i]);
            int len2 = list2.size();
            if(len2>0){
                String [] idss=new String[len2];
                for(int j=0;j<len2;j++){
                    idss[j]=list2.get(j).getUid().toString();
                    list_Id.add(list2.get(j).getUid());
                }

                usersMapper.deleteUserRole(idss);
            }

        }
        int result = roleMapper.deleteRolesByIds(split);
        //配置用户的角色，默认都是普通用户,为删除权限的用户配置默认角色
        int len =list_Id.size();
        for(int i=0;i<len;i++){
            String[] roles = new String[]{"2"};
             roleMapper.addRoles(roles, list_Id.get(i));
        }

        return result == split.length;
    }

    public int updateRoleById(Role role) {
        return roleMapper.updateRolesById(role);
    }
//  添加角色
    public int addRole(Role role) {
//        判断角色是否存在
        List<Role> list=roleMapper.getRoleByName(role.getRole());
        if(list.size()>0){
            return -1;
        }
        List<Role> listEn=roleMapper.getRoleByEnName(role.getRole_en());
        if(listEn.size()>0){
            return -1;
        }

        if(role.getPriv()==null || role.getPriv()==""){
            return -2;
        }

        int result=roleMapper.addRole(role);
        int rid=roleMapper.getRoleId(role);

        if(role.getPriv()!=null && role.getPriv()!="") {
            String priv = role.getPriv();
            String[] split = priv.split(",");
            int length = split.length;
            int i = 0;
            for (; i < length; i++) {
                String[] split2 = split[i].split("_");
                int mid = privMapper.getModuleIdByName(split2[0]);
//                若mid为0，表示之前没有，则先插入
                if (mid == 0) {
                    Module module = new Module();
                    module.setModule(split2[0]);
                    privMapper.addModule(module);
                    mid = privMapper.getModuleIdByName(split2[0]);

                }
                int oid = privMapper.getOperationIdByName(split2[1]);
//                若oid为0，表示之前没有，则先插入
                if (oid == 0) {
                    Operation operation = new Operation();
                    operation.setOperation(split2[1]);
                    privMapper.addOperation(operation);
                    oid = privMapper.getOperationIdByName(split2[1]);
                }
                List<Module> list1 = privMapper.getSpecificModules(mid, oid);
                int moid;
                if (list1.size() == 0) {
                    privMapper.addPriv(mid, oid);
                    List<Module> list2 = privMapper.getSpecificModules(mid, oid);
                    moid = (list2.get(0).getId()).intValue();
                } else {
                    moid = (list1.get(0).getId()).intValue();
                }

                List<Role> list_ = roleMapper.getRolePriv(rid, moid);
                if (list_.size() == 0) {
                    roleMapper.addRolePriv(rid, moid);
                }
            }
        }

        return result;
    }

//    修改role与priv之间的关系
    public int updateRolePriv(Role role){
        int resultCode = -1;
//        得到角色id
        int rid = role.getId().intValue();
        String []rids=new String[1];
        rids[0]=rid+"";

        roleMapper.deleteRolesPrivs(rids);

        if(role.getPriv()!=null){
            String priv=role.getPriv();
            String[] split =priv.split(",");
            int length = split.length;
            int i=0;
            for(;i<length;i++){
                String [] split2 = split[i].split("_");
                int mid=privMapper.getModuleIdByName(split2[0]);
//                若mid为0，表示之前没有，则先插入
                if(mid==0){
                    Module module = new Module();
                    module.setModule(split2[0]);
                    int result=privMapper.addModule(module);
                    mid=privMapper.getModuleIdByName(split2[0]);

                }
                int oid=privMapper.getOperationIdByName(split2[1]);
//                若oid为0，表示之前没有，则先插入
                if(oid==0){
                    Operation operation=new Operation();
                    operation.setOperation(split2[1]);
                    int result=privMapper.addOperation(operation);
                    oid=privMapper.getOperationIdByName(split2[1]);
                }
                List<Module> list1=privMapper.getSpecificModules(mid,oid);
                int moid;
                if(list1.size()==0){
                    privMapper.addPriv(mid,oid);
                    List<Module> list2=privMapper.getSpecificModules(mid,oid);
                    moid=(list2.get(0).getId()).intValue();
                }
                else{
                    moid=(list1.get(0).getId()).intValue();
                }

                List<Role> list_=roleMapper.getRolePriv(rid,moid);
                if(list_.size()==0){
                    roleMapper.addRolePriv(rid,moid);
                }
            }
            if(i==length){
                resultCode=1;
            }
        }
        return resultCode;

    }
//用角色id查询权限
    public List<Module> getRolePrivs(String id){
        int i = Integer.parseInt(id);
        return roleMapper.getRolePrivById(i);
    }


    //得到所有角色的数量
    public int getRolesCount( Role role) {
        return roleMapper.getRolesCount(role);
    }
}
